!(function () {
    /*F7*/
    var smapp = new Framework7({
        root : "#smapp",
        name : "代言人中心",
        version : "2.0.0",
        view : {
            pushState:true
        },
        dialog:{
            buttonOk:"确认",
            buttonCancel:"取消"
        },
        routes : [
            {
                path : "/home/",
                url  : "./pages/home.html"
            },
            {
                path : "/customer/",
                url  : "./pages/customer.html"
            },
            {
                path : "/product/",
                url  : "./pages/product.html"
            },
            {
                path : "/productmedia/:gid",
                templateUrl  : "./pages/productmedia.html"
            },
            {
                path : "/invi/",
                url  : "./pages/invi.html"
            },
            {
                path : "/inviguide/",
                url  : "./pages/inviguide.html"
            },
            {
                path : "/coupon/",
                url  : "./pages/coupon.html"
            },
            {
                path : "/couponshare/:couponid/:couponvalue",
                templateUrl  : "./pages/couponshare.html"
            },
            {
                path : "/article/",
                url  : "./pages/article.html"
            },
            {
                path : "/post/",
                url  : "./pages/post.html"
            },
            {
                path : "/sharerank/",
                url  : "./pages/sharerank.html"
            },
            {
                path : "/money/",
                url  : "./pages/money.html"
            },
            {
                path : "/moneyrecord/",
                url  : "./pages/moneyrecord.html"
            },
            {
                path : "/sharevisitor/",
                url  : "./pages/sharevisitor.html"
            }
        ]

    });
    var smMainView = smapp.views.create('.view-main',{});
    initwxsdk();
    /*全局变量*/
    var $$ = Dom7;
    var Globalsls=0;
    var Globalopenid="";
    var GlobalallowInfinite = true;
    var Globalpageno = 1;
    var Globalpagesize = 20;
    //分享榜单滚动参数
    var GlobalallowInfinitetodayrank,Globalpagenotodayrank,GlobalallowInfinitelastdayrank,Globalpagenolastdayrank;
    //访问记录滚动参数
    var GlobalallowInfinitepost,Globalpagenopost,GlobalallowInfiniteart,Globalpagenoart;
    /*模板编译*/
    var homeUserInfoTemplate = $$('script#Userinfo').html();
    var compiledHomeUserInfoTemplate = Template7.compile(homeUserInfoTemplate);
    var customerItemTemplate = $$('script#Customeritem').html();
    var compiledCustomerItemTemplate = Template7.compile(customerItemTemplate);
    var productItemTemplate = $$('script#Productitem').html();
    var compiledProductItemTemplate = Template7.compile(productItemTemplate);
    var productMediaImgTemplate = $$('script#Productmediaimg').html();
    var compiledProductMediaImgTemplate = Template7.compile(productMediaImgTemplate);
    var productMediaTextTemplate = $$('script#Productmediatext').html();
    var compiledProductMediaTextTemplate = Template7.compile(productMediaTextTemplate);
    var productShareLinkTemplate = $$('script#Productsharelink').html();
    var compiledProductShareLinkTemplate = Template7.compile(productShareLinkTemplate);
    var inviPostTemplate = $$('script#Invipost').html();
    var compiledInviPostTemplate = Template7.compile(inviPostTemplate);
    var couponItemTemplate = $$('script#Couponitem').html();
    var compiledCouponItemTemplate = Template7.compile(couponItemTemplate);
    var noCouponTemplate = $$('script#Nocoupon').html();
    var compiledNoCouponTemplate = Template7.compile(noCouponTemplate);
    var articleItemTemplate = $$('script#Articleitem').html();
    var compiledArticleItemTemplate = Template7.compile(articleItemTemplate);
    var postItemTemplate = $$('script#Postitem').html();
    var compiledPostItemTemplate = Template7.compile(postItemTemplate);
    var postTypeItemTemplate = $$('script#Posttypeitem').html();
    var compiledPostTypeItemTemplate = Template7.compile(postTypeItemTemplate);
    var rankItemTemplate = $$('script#Rankitem').html();
    var compiledRankItemTemplate = Template7.compile(rankItemTemplate);
    var shareStatusTemplate = $$('script#Sharestatus').html();
    var compiledShareStatusTemplate = Template7.compile(shareStatusTemplate);
    var moneyRecordItemTemplate = $$('script#MoneyRecorditem').html();
    var compiledMoneyRecordItemTemplate = Template7.compile(moneyRecordItemTemplate);
    var articleVisitorItemTemplate = $$('script#Articlevisitoritem').html();
    var compiledArticleVisitorItemTemplate = Template7.compile(articleVisitorItemTemplate);
    var postScannerItemTemplate = $$('script#Postscanneritem').html();
    var compiledPostScannerItemTemplate = Template7.compile(postScannerItemTemplate);
    /*页面事件*/
    $$(document).on('page:init','.page[data-name="Home"]',function () {
        ////console.log('page home init');
        initHome();
    });

    $$(document).on('page:init','.page[data-name="Customer"]',function () {
        //console.log('page customer init');
        GlobalallowInfinite = true;
        Globalpageno = 1;
        Globalpagesize = 20;
        // getCustomerList();

        $$('.infinite-scroll-content').on('infinite', function () {
            if (!GlobalallowInfinite) return;
            GlobalallowInfinite = false;
            getCustomerList();
        });
        $$('.infinite-scroll-content').trigger('infinite');
    });
    $$(document).on('page:init','.page[data-name="Product"]',function () {
        //console.log('page product init');
        GlobalallowInfinite = true;
        Globalpageno = 1;
        Globalpagesize = 20;
        // getProductList();

        $$('.infinite-scroll-content').on('infinite', function () {
            if (!GlobalallowInfinite) return;
            GlobalallowInfinite = false;
            getProductList();
        });
        $$('.infinite-scroll-content').trigger('infinite');
    });
    $$(document).on('page:init','.page[data-name="Productmedia"]',function () {
        //console.log('page productmeida init');
        var gid = $$('.page[data-name="Productmedia"]').data('gid');
        getProductMedia(gid);
    });
    $$(document).on('page:afterout','.page[data-name="Productmedia"]',function () {
        //console.log('page productmedia out');
        var title= '能量逗代言人中心';
        var desc = '欢迎加入能量逗代言人的大家庭~~';
        var url = 'http://api.nldou.com/ucenter/weixinlogin?pid=53281';
        setshare(title,desc,url,'http://wap.nldou.com/img/nldou-bg.jpg');
    });
    $$(document).on('page:init','.page[data-name="Invi"]',function(){
        //console.log('page invi init');
        getInviPosts();
    });
    $$(document).on('page:init','.page[data-name="Coupon"]',function(){
        //console.log('page coupon init');
        getCouponList();
    });
    $$(document).on('page:init','.page[data-name="Couponshare"]',function(){
        //console.log('page couponshare init');
        var couponid=$$('.page[data-name="Couponshare"]').data('couponid');
        var couponvalue=$$('.page[data-name="Couponshare"]').data('couponvalue');
        var url= 'http://api.nldou.com/ucenter/weixinlogin?pid=5626&page=&param=alias.'+Globalsls+'$'+couponid;
        var title= '点击领取'+couponvalue+'元优惠券';
        var desc = '能量逗益智家庭桌游，快乐教育，与孩子共同成长';
        setshare(title,desc,url,'http://wap.nldou.com/img/nldou-bg.jpg');
    });
    $$(document).on('page:afterout','.page[data-name="Couponshare"]',function(){
        //console.log('page couponshare out');
        var title= '能量逗代言人中心';
        var desc = '欢迎加入能量逗代言人的大家庭~~';
        var url = 'http://api.nldou.com/ucenter/weixinlogin?pid=53281';
        setshare(title,desc,url,'http://wap.nldou.com/img/nldou-bg.jpg');
    });
    $$(document).on('page:init','.page[data-name="Article"]',function () {
        //console.log('page article init');
        GlobalallowInfinite = true;
        Globalpageno = 1;
        Globalpagesize = 10;
        // getArticleList();

        $$('.infinite-scroll-content').on('infinite', function () {
            if (!GlobalallowInfinite) return;
            GlobalallowInfinite = false;
            getArticleList();
        });
        $$('.infinite-scroll-content').trigger('infinite');
    });
    $$(document).on('page:init','.page[data-name="Post"]',function () {
        //console.log('page post init');
        getPostType();
        GlobalallowInfinite = true;
        Globalpageno = 1;
        Globalpagesize = 10;
        // getPostList(0);
        $$('.infinite-scroll-content').on('infinite', function () {
            if (!GlobalallowInfinite) return;
            GlobalallowInfinite = false;
            getPostList(0);
        });
        $$('.infinite-scroll-content').trigger('infinite');
    });
    $$(document).on('page:init','.page[data-name="Sharerank"]',function () {
        //console.log('page sharerank init');
        GlobalallowInfinitetodayrank = true;
        GlobalallowInfinitelastdayrank = true;
        Globalpagenotodayrank = 1;
        Globalpagenolastdayrank = 1;
        Globalpagesize = 20;

        $$('#ranklist-today').on('infinite', function () {
            //console.log('today data pull');
            if (!GlobalallowInfinitetodayrank) return;
            GlobalallowInfinitetodayrank = false;
            getRankList();
        });
        $$('#ranklist-lastday').on('infinite', function () {
            //console.log('lastday data pull');
            if (!GlobalallowInfinitelastdayrank) return;
            GlobalallowInfinitelastdayrank = false;
            getLastDayRankList();
        });
        $$('#ranklist-today').trigger('infinite');
        $$('#ranklist-lastday').trigger('infinite');
    });

    $$(document).on('page:init','.page[data-name="Money"]',function () {
        smapp.request.get('http://api.nldou.com/salesman/share/getRemainMoney',{sls:Globalsls},function (res) {
            if (res.code==1){
                var html = compiledShareStatusTemplate(res.data);
                $$('.page[data-name="Money"] .share-status-container').html(html);
            }
        },function () {},'json');
    });
    $$(document).on('page:init','.page[data-name="Moneyrecord"]',function () {
        smapp.request.get('http://api.nldou.com/salesman/share/getMoneyRecord',{sls:Globalsls},function (res) {
            if (res.code==1){
                var html=compiledMoneyRecordItemTemplate(res.data);
                $$('.page[data-name="Moneyrecord"] .money-record-wrap').html(html);
            }
        },function () {},'json');
    });
    $$(document).on('page:init','.page[data-name="Sharevisitor"]',function () {
        GlobalallowInfiniteart = true;
        GlobalallowInfinitepost = true;
        Globalpagenoart = 1;
        Globalpagenopost = 1;
        Globalpagesize = 20;

        $$('#article-visitor').on('infinite', function () {
            //console.log('today data pull');
            if (!GlobalallowInfiniteart) return;
            GlobalallowInfiniteart = false;
            getArticleVisitorkList();
        });
        $$('#post-scanner').on('infinite', function () {
            //console.log('lastday data pull');
            if (!GlobalallowInfinitepost) return;
            GlobalallowInfinitepost = false;
            getPostScannerList();
        });
        $$('#article-visitor').trigger('infinite');
        $$('#post-scanner').trigger('infinite');
    });
    /*事件*/
//修改头像
    $$(document).on('click','.editiconbtn',function (){
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                changeicon(res.localIds[0]);
            }
        });
    });
//累积客户
    $$(document).on('click','.customer-panel-btn',function () {
        smMainView.router.navigate('/customer/');
    });
//累积邀请
    $$(document).on('click','.invilist-panel-btn',function () {
        window.location.href="https://wap.youzan.com/salesman/invitation/invitation/gethtml?kdt_id=18168297";
    });
//推广订单
    $$(document).on('click','.tradelist-panel-btn',function () {
        window.location.href="https://wap.youzan.com/salesman/order/order/gethtml?kdt_id=18168297";
    });
//推广产品
    $$(document).on('click','.product-panel-btn',function () {
        smMainView.router.navigate('/product/');
    });
    $$(document).on('click','.share-to-friend-btn',function () {
        //console.log('share-to-friend-btn click');
        var title= $$(this).data('title');
        var desc = $$(this).data('desc');
        var url = $$(this).data('url');
        var imgurl = $$(this).data('imgurl');
        setshare(title,desc,url,imgurl);
    });
    $$(document).on('click','.share-to-moments-btn',function () {
        //console.log('share-to-moments-btn click');
        var title= $$(this).data('title');
        var desc = $$(this).data('desc');
        var url = $$(this).data('url');
        var imgurl = $$(this).data('imgurl');
        setshare(title,desc,url,imgurl);
    });
//邀请他人
    $$(document).on('click','.invi-panel-btn',function () {
        smMainView.router.navigate('/invi/');
    });
//优惠券
    $$(document).on('click','.coupon-panel-btn',function () {
        smMainView.router.navigate('/coupon/');
    });
//专属推文
    $$(document).on('click','.article-panel-btn',function () {
        smMainView.router.navigate('/article/');
    });
    $$(document).on('click','.article-item',function () {
        var model=$$(this).data('artmodel');
        window.location.href="http://api.nldou.com/salesman/article/create/"+model+"."+Globalsls;
    });
//专属海报
    $$(document).on('click','.post-panel-btn',function () {
        smMainView.router.navigate('/post/');
    });
    $$(document).on('click','.post-item',function () {
        var imgsrc = $$(this).data('src');
        var imghtml = "<img src='"+imgsrc+"' style='width: 80%;'>";
        $$('.preview-img-popup .img-container').html(imghtml);
    });
    $$(document).on('click','.posttype-item',function () {
        $$('.posttype-item').removeClass('active');
        $$(this).addClass('active');
        //清除之前的事件
        smapp.infiniteScroll.destroy('.infinite-scroll-content');
        $$('.infinite-scroll-content').off('infinite');
        $$('.infinite-scroll-preloader').remove();
        var html = '<div style="margin: 20px 0 0;min-height: 100vh;"><div class="post-list flex-row flex-align-center flex-justify-start flex-wrap" style="padding: 10px 0;"></div></div><div class="preloader infinite-scroll-preloader"></div>';
        $$('.page[data-name="Post"] .page-content').html(html);
        //重新绑定事件
        GlobalallowInfinite = true;
        Globalpageno = 1;
        Globalpagesize = 10;
        smapp.infiniteScroll.create('.infinite-scroll-content');
        $$('.infinite-scroll-content').on('infinite', function () {
            if (!GlobalallowInfinite) return;
            GlobalallowInfinite = false;
            var type = $$('.posttype-item.active').data('type');
            getPostList(type);
        });
        $$('.infinite-scroll-content').trigger('infinite');
    });
//分享排行榜
    $$(document).on('click','.rank-panel-btn',function () {
        smMainView.router.navigate('/sharerank/');
    });
//提现海报文章收益
    $$(document).on('click','.demand-money-btn',function () {
        var money=$$('.page[data-name="Money"] .remain-money').data('money');
        money=parseFloat(money);
        if (money<10){
            smapp.dialog.alert('可提现金额小于10元不可提现','');
            return;
        }
        smapp.dialog.prompt('请输入提现金额','',function (val) {
            if (!checkMoney(val)){
                smapp.dialog.alert('请输入正确的数字','');
                return;
            }
            val=parseFloat(val);
            if (val>money) {
                smapp.dialog.alert('您最多只能提现'+money+'元','');
                return;
            }
            smapp.dialog.preloader('提交中');
            val*=100;
            smapp.request.get("http://api.nldou.com/salesman/share/demandMoney",{sls:Globalsls,money:val},function (res) {
                smapp.dialog.close();
                if (res.data.status==1){
                    smapp.dialog.alert('提交成功','',function () {
                        $$('.page[data-name="Money"] .remain-money').html('￥ '+res.data.remainmoney);
                        $$('.page[data-name="Money"] .remain-money').data('money',res.data.remainmoney);
                    });
                }
                if (res.data.status==2) smapp.dialog.alert('可提现金额小于10元不可提现','');
                if (res.data.status==3) smapp.dialog.alert('提现金额超过可提现额度','');
            },function () {smapp.dialog.close();},'json');
        });
    });

    /*工具函数*/
    function checklogin() {
        //console.log('checklogin');
        smapp.request({
            async:false,
            method:'GET',
            url:"http://api.nldou.com/salesman/passport/checklogin",
            data:{},
            dataType:'json',
            xhrFields:{"withCredentials":true},
            success:function (res) {
                //console.log(res);
                if (res.code==1){
                    Globalsls=res.data.sls;
                    Globalopenid=res.data.openid;
                    if (Globalsls=='noauth') loginscreen();
                    else toHome();
                }else{
                    //session不存在
                    window.location.href='http://api.nldou.com/ucenter/weixinlogin?pid=53281';
                }
            }
        })
    }
    function initwxsdk(){
        smapp.request({
            url:"http://api.nldou.com/weixinapi/init_wx_jsapi",
            method:"GET",
            data: {fromUrl:window.location.href},
            dataType:'json',
            success: function (res) {
                if(res.code==1){
                    wx.config({
                        debug: false,
                        appId: res.data.appid,
                        timestamp: res.data.timestamp,
                        nonceStr: res.data.nonceStr,
                        signature: res.data.signature,
                        jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','hideMenuItems','chooseImage'
                            ,'uploadImage','getLocalImgData']
                    });
                }
            }
        });
        wx.ready(function () {
            wx.hideMenuItems({
                menuList: ['menuItem:share:qq', 'menuItem:share:weiboApp', 'menuItem:share:facebook', 'menuItem:share:QZone'
                    ,'menuItem:copyUrl', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari']
            });
            wx.onMenuShareTimeline({
                title: '能量逗代言人中心',
                link: 'http://api.nldou.com/ucenter/weixinlogin?pid=53281',
                imgUrl: 'http://wap.nldou.com/img/nldou-bg.jpg'
            });
            wx.onMenuShareAppMessage({
                title: '能量逗代言人中心',
                desc: '欢迎加入能量逗代言人的大家庭~~',
                link: 'http://api.nldou.com/ucenter/weixinlogin?pid=53281',
                imgUrl: 'http://wap.nldou.com/img/nldou-bg.jpg',
                type: 'link'
            });
        });
    }
    function setshare(title,desc,url,imgurl) {
        wx.onMenuShareTimeline({
            title: title,
            link: url,
            imgUrl: imgurl
        });
        wx.onMenuShareAppMessage({
            title: title,
            desc: desc,
            link: url,
            imgUrl: imgurl,
            type: 'link'
        });
    }
    function loginscreen() {
        //console.log('loginscreen');
        smapp.dialog.prompt(
            "请输入手机号码","",function(mobile){
                if (!(/^1\d{10}$/).test(mobile)) {
                    smapp.dialog.alert("请输入正确的11位手机号","",loginscreen);
                    return;
                }
                smapp.request({
                    async:false,
                    method:'GET',
                    url:"http://api.nldou.com/salesman/passport/firstlogin",
                    data:{mobile:mobile},
                    dataType:'json',
                    xhrFields:{"withCredentials":true},
                    success:function(res){
                        //console.log(res);
                        if (res.code==1){
                            if (res.data.status==1){
                                Globalsls=res.data.data.sls;
                                Globalopenid=res.data.data.openid;
                                toHome();
                            }else if(res.data.status==2){
                                //不是销售员，跳转申请页面
                                var dialog = smapp.dialog.create({
                                    title:"",
                                    text:"您还不是能量逗代言人，请先申请成为代言人",
                                    buttons:[
                                        {
                                            text:"去申请",
                                            onClick: function () {
                                                window.location.href='https://wap.youzan.com/salesman/home/tutorial/index?kdt_id=18168297';
                                            }
                                        }
                                    ]
                                });
                                dialog.open();
                            }else if(res.data.status==3){
                                //session不存在
                                window.location.href='http://api.nldou.com/ucenter/weixinlogin?pid=53281';
                            }
                        }
                    }
                })
            }
        )
    }
    function toHome() {
        smMainView.router.navigate('/home/',{reloadCurrent:true});
        kp.stop();
    }
    function changeicon(localid)
    {
        //生成预览
        wx.getLocalImgData({
            localId: localid,
            success: function (res) {
                var localData = res.localData;
                // alert(localData);
                $$('.page[data-name="Home"] .editiconbtn img').attr('src',localData);
            }
        });
        //上传到服务器
        wx.uploadImage({
            localId: localid,
            isShowProgressTips: 1,
            success: function (res) {
                uploadimage(res.serverId);
            }
        });
    }
    function uploadimage(mediaid)
    {
        smapp.request.post("http://api.nldou.com/salesman/passport/changeIcon",
            {sls:Globalsls,mediaid:mediaid},
            function (res){},function () {},'json');
    }
    function initHome(){
        smapp.request.post(
            'http://api.nldou.com/salesman/passport/getUserInfo',
            {wx_openid:Globalopenid},
            function (res) {
                //console.log(res);
                if(res.code==1){
                    var html = compiledHomeUserInfoTemplate(res.data);
                    $$('.page[data-name="Home"] .userinfo-container').html(html);
                }
            },
            function () {},
            'json');
        smapp.request.post(
            'http://api.nldou.com/salesman/passport/getUserProfit',
            {sls:Globalsls},
            function (res) {
                //console.log(res);
                if(res.code==1){
                    $$('.page[data-name="Home"] .userprofit-container .userProfitNum').html(res.data.profit);
                    $$('.page[data-name="Home"] .userprofit-container .userOrderAmount').html(res.data.orderamount);
                }
            },
            function () {},
            'json');
        smapp.request.post(
            'http://api.nldou.com/salesman/passport/getUserCustomerNum',
            {sls:Globalsls},
            function (res) {
                //console.log(res);
                if(res.code==1){
                    $$('.page[data-name="Home"] .customer-panel-btn .userCustomerNum').html(res.data.cusnum);
                }
            },
            function () {},
            'json');
        smapp.request.post(
            'http://api.nldou.com/salesman/passport/getUserInviNum',
            {sls:Globalsls},
            function (res) {
                //console.log(res);
                if(res.code==1){
                    $$('.page[data-name="Home"] .invilist-panel-btn .userInviNum').html(res.data.invinum);
                }
            },
            function () {},
            'json');
        smapp.request.post(
            'http://api.nldou.com/salesman/passport/getUserTradesNum',
            {sls:Globalsls},
            function (res) {
                //console.log(res);
                if(res.code==1){
                    $$('.page[data-name="Home"] .tradelist-panel-btn .userTradesNum').html(res.data.tradesnum);
                }
            },
            function () {},
            'json');
    }
    function getCustomerList(){
        smapp.request.get(
            'http://api.nldou.com/salesman/passport/getCustomerList',
            {sls:Globalsls,pageno:Globalpageno,pagesize:Globalpagesize},
            function (res) {
                if (res.code == 1) {
                    var html = compiledCustomerItemTemplate(res.data.list);
                    $$('.page[data-name="Customer"] .customer-list ul').append(html);
                    Globalpageno++;
                }
                if (res.data.end==1){
                    smapp.infiniteScroll.destroy('.infinite-scroll-content');
                    $$('.infinite-scroll-preloader').remove();
                }
                GlobalallowInfinite=true;
            },function () {GlobalallowInfinite=true;},'json');
    }
    function getCouponList() {
        smapp.request.get(
            'http://api.nldou.com/salesman/coupon/getCoupons',
            {sls:Globalsls},
            function (res) {
                if (res.code == 1) {
                    if (res.data.status == 1) {
                        var html = compiledCouponItemTemplate(res.data.data);
                        $$('.page[data-name="Coupon"] .coupon-container ul').html(html);
                        $$('.page[data-name="Coupon"] .coupon-item').on('click',function () {
                            var couponid = $$(this).data('couponid');
                            var couponvalue = $$(this).data('couponvalue');
                            smMainView.router.navigate('/couponshare/'+couponid+'/'+couponvalue);
                        })
                    }else{
                        var html = compiledNoCouponTemplate();
                        $$('.page[data-name="Coupon"] .coupon-container').html(html);
                    }
                }
            },function () {},'json');
    }
    function getArticleList(){
        smapp.request.post(
            'http://api.nldou.com/salesman/article/getArticles',
            {page:Globalpageno,pagesize:Globalpagesize},
            function (res) {
                if (res.code == 1) {
                    var html = compiledArticleItemTemplate(res.data.list);
                    $$('.page[data-name="Article"] .article-container ul').append(html);
                    Globalpageno++;
                }
                if (res.data.end==1){
                    smapp.infiniteScroll.destroy('.infinite-scroll-content');
                    $$('.infinite-scroll-preloader').remove();
                }
                GlobalallowInfinite=true;
            },function () {GlobalallowInfinite=true;},'json');
    }
    function getProductList(){
        smapp.request.post(
            'http://api.nldou.com/salesman/product/getProductList',
            {pageno:Globalpageno,pagesize:Globalpagesize},
            function (res) {
                if (res.code == 1) {
                    var html = compiledProductItemTemplate(res.data.list);
                    $$('.page[data-name="Product"] .product-list ul').append(html);
                    Globalpageno++;
                }
                if (res.data.end==1){
                    smapp.infiniteScroll.destroy('.infinite-scroll-content');
                    $$('.infinite-scroll-preloader').remove();
                }
                GlobalallowInfinite=true;
            },function () {GlobalallowInfinite=true;},'json');
    }
    function getProductMedia(gid){
        smapp.request.post(
            'http://api.nldou.com/salesman/product/getMedia',
            {gid:gid,sls:Globalsls},
            function (res) {
                if (res.code == 1) {
                    var imghtml = compiledProductMediaImgTemplate(res.data.img);
                    $$('.page[data-name="Productmedia"] .swiper-wrapper').html(imghtml);
                    var texthtml = compiledProductMediaTextTemplate(res.data.text);
                    $$('.page[data-name="Productmedia"] .product-media-text-container').html(texthtml);
                    var sharelinkhtml = compiledProductShareLinkTemplate(res.data.share);
                    $$('.page[data-name="Productmedia"] .product-share-link-container').html(sharelinkhtml);
                    smapp.swiper.create('.swiper-container',{
                        height:200,
                        slidesPerView:'auto',
                        spaceBetween:30,
                        centeredSlides: true,
                        freeMode:true
                    });
                    var clipboard = new ClipboardJS('.copy-btn');
                    clipboard.on('success', function(e) {
                        //console.log('copy succ');
                        var dialog = smapp.dialog.create({title:"复制成功！"});
                        dialog.open();
                        setTimeout(function (){dialog.close();dialog.destroy();},2000);
                    });
                    clipboard.on('error', function(e) {
                        //console.log('copy error');
                        var dialog = smapp.dialog.create({title:"复制失败请重试！"});
                        dialog.open();
                        setTimeout(function (){dialog.close();dialog.destroy();},2000);
                    });
                    $$('.to-product-detail-btn').attr('href',res.data.share.link);
                    setshare(res.data.share.title,res.data.share.desc,res.data.share.link,res.data.share.cover);
                }
            },function () {},'json');
    }
    function getInviPosts()
    {
        smapp.dialog.preloader('海报生成中');
        smapp.request.get(
            'http://api.nldou.com/salesman/invi/getInviPosts',
            {sls:Globalsls},
            function (res) {
                if (res.code == 1) {
                    var html = compiledInviPostTemplate(res.data);
                    $$('.page[data-name="Invi"] .swiper-wrapper').html(html);
                    smapp.swiper.create('.swiper-container',{
                        slidesPerView:'auto',
                        spaceBetween:30,
                        centeredSlides: true,
                        freeMode:true
                    });
                }
                smapp.dialog.close();
            },function () {smapp.dialog.close();},'json');
    }
    function getPostList(type)
    {
        smapp.request.post(
            'http://api.nldou.com/salesman/postv2/getPosts',
            {sls:Globalsls,type:type,pageno:Globalpageno,pagesize:Globalpagesize},
            function (res) {
                if (res.code == 1) {
                    var html = compiledPostItemTemplate(res.data.list);
                    $$('.page[data-name="Post"] .post-list').append(html);
                    Globalpageno++;
                }
                if (res.data.end==1){
                    smapp.infiniteScroll.destroy('.infinite-scroll-content');
                    $$('.infinite-scroll-preloader').remove();
                }
                GlobalallowInfinite=true;
            },function () {},'json');
    }
    function getPostType()
    {
        smapp.request.get(
            'http://api.nldou.com/salesman/postv2/getPostType',{},
            function (res) {
                if (res.code == 1) {
                    var html = compiledPostTypeItemTemplate(res.data);
                    $$('.page[data-name="Post"] .subnavbar .segmented').html(html);
                }
            },function () {},'json');
    }
    function getRankList() {
        smapp.request.post(
            'http://api.nldou.com/salesman/share/getRankList',
            {pageno:Globalpagenotodayrank,pagesize:Globalpagesize},
            function (res) {
                if (res.code == 1) {
                    var html = compiledRankItemTemplate(res.data.list);
                    $$('.page[data-name="Sharerank"] .today-rank-list ul').append(html);
                    if (Globalpagenotodayrank==1){
                        var notify = smapp.notification.create({
                            title: '榜单说明',
                            subtitle: '统计周期为'+res.data.time,
                            text: '以代言人分享的海报和推文的阅读人次为排序依据',
                            closeButton: true
                        });
                        notify.open();
                    }
                    Globalpagenotodayrank++;
                }
                if (res.data.end==1){
                    smapp.infiniteScroll.destroy('#ranklist-today');
                    $$('#ranklist-today .infinite-scroll-preloader').remove();
                }
                GlobalallowInfinitetodayrank=true;
            },function () {GlobalallowInfinitetodayrank=true;},'json');
    }
    function getLastDayRankList() {
        smapp.request.post(
            'http://api.nldou.com/salesman/share/getLastDayRankList',
            {pageno:Globalpagenolastdayrank,pagesize:Globalpagesize},
            function (res) {
                if (res.code == 1) {
                    var html = compiledRankItemTemplate(res.data.list);
                    $$('.page[data-name="Sharerank"] .lastday-rank-list ul').append(html);
                    Globalpagenolastdayrank++;
                }
                if (res.data.end==1){
                    smapp.infiniteScroll.destroy('#ranklist-lastday');
                    $$('#ranklist-lastday .infinite-scroll-preloader').remove();
                }
                GlobalallowInfinitelastdayrank=true;
            },function () {GlobalallowInfinitelastdayrank=true;},'json');
    }
    function getArticleVisitorkList() {
        smapp.request.post(
            'http://api.nldou.com/salesman/share/getShareArticleVistor',
            {sls:Globalsls,pageno:Globalpagenoart,pagesize:Globalpagesize},
            function (res) {
                if (res.code == 1) {
                    var html = compiledArticleVisitorItemTemplate(res.data.list);
                    $$('.page[data-name="Sharevisitor"] .article-visitor-list ul').append(html);
                    Globalpagenoart++;
                }
                if (res.data.end==1){
                    smapp.infiniteScroll.destroy('#article-visitor');
                    $$('#article-visitor .infinite-scroll-preloader').remove();
                }
                GlobalallowInfiniteart=true;
            },function () {GlobalallowInfiniteart=true;},'json');
    }
    function getPostScannerList() {
        smapp.request.post(
            'http://api.nldou.com/salesman/share/getSharePostScanner',
            {sls:Globalsls,pageno:Globalpagenopost,pagesize:Globalpagesize},
            function (res) {
                if (res.code == 1) {
                    var html = compiledPostScannerItemTemplate(res.data.list);
                    $$('.page[data-name="Sharevisitor"] .post-scanner-list ul').append(html);
                    Globalpagenopost++;
                }
                if (res.data.end==1){
                    smapp.infiniteScroll.destroy('#post-scanner');
                    $$('#post-scanner .infinite-scroll-preloader').remove();
                }
                GlobalallowInfinitepost=true;
            },function () {GlobalallowInfinitepost=true;},'json');
    }
    function checkMoney(val)
    {
        return (/(^[1-9][0-9]*$)|(^[0]\.[1-9]{1}$)|(^[1-9][0-9]*\.[1-9]{1}$)/).test(val);
    }
    /*开屏*/
    function Loginanim(speed){
        this.delta=0;
        this.speed=speed;
        this.animid=0;
    }
    Loginanim.prototype={
        constructor:Loginanim,
        anim:function () {
            this.delta+=this.speed;
            var d0 = Math.abs(Math.sin(Math.PI/2+this.delta));
            var d1 = Math.abs(Math.sin(Math.PI/3+this.delta));
            var d2 = Math.abs(Math.sin(Math.PI/6+this.delta));
            $$('.login-anim-btn').eq(0).css({'opacity':d0});
            $$('.login-anim-btn').eq(1).css({'opacity':d1});
            $$('.login-anim-btn').eq(2).css({'opacity':d2});
            this.animid=requestAnimationFrame(this.anim.bind(this));
        },
        start:function () {
            this.animid = requestAnimationFrame(this.anim.bind(this));
        },
        stop:function () {
            cancelAnimationFrame(this.animid);
        }
    };
    var kp = new Loginanim(0.03);
    kp.start();
    checklogin();
})();